package com.yixing.demo.controller;

import com.yixing.demo.constants.Constants;
import com.yixing.demo.model.LoveMusic;
import com.yixing.demo.model.Music;
import com.yixing.demo.model.Result;
import com.yixing.demo.model.User;
import com.yixing.demo.service.LoveMusicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/loveMusic")
public class LoveMusicController {
    @Autowired
    private LoveMusicService loveMusicService;
    @RequestMapping("/increaseLoveMusic")
    public Result<Boolean> increaseLoveMusic(Integer musicId, HttpSession session) {
        User userInfo = (User)session.getAttribute(Constants.USER_SESSION_KEY);
        Integer userId = userInfo.getId();
        LoveMusic loveMusic = loveMusicService.inspectMusicByDoubleId(userId, musicId);
        if(loveMusic != null) {
            return new Result<>(-1, "歌曲以前已添加到我喜欢列表，不能再次添加！", false);
        }else {
            if(loveMusicService.increaseLoveMusic(userId, musicId) == 1) {
                return new Result<>(1, "已添加到我喜欢", true);
            }else {
                return new Result<>(-1, "添加到我喜欢失败", false);
            }
        }
    }

    @RequestMapping("/seekLoveMusic")
    public Result<List<Music>> seekLoveMusic(@RequestParam(required = false) String title, HttpSession session) {
        List<Music> list = null;
        User userInfo = (User)session.getAttribute(Constants.USER_SESSION_KEY);
        Integer userId = userInfo.getId();
        if(title == null) {
            list = loveMusicService.seekLoveMusicByNoTitle(userId);
        }else {
            list = loveMusicService.seekLoveMusicByTitle(userId, title);
        }
        return new Result<>(1, "查询喜欢的歌曲成功", list);
    }

    @RequestMapping("/deleteLoveMusic")
    public Result<Boolean> deleteLoveMusic(Integer musicId, HttpSession session) {
        User userInfo = (User)session.getAttribute(Constants.USER_SESSION_KEY);
        Integer userId = userInfo.getId();
        LoveMusic loveMusic = loveMusicService.inspectMusicByDoubleId(userId, musicId);
        if(loveMusic == null) {
            return new Result<>(-1, "没有你要移除的喜欢的歌曲！", false);
        }
        Integer ret = loveMusicService.deleteLoveMusic(userId, musicId);
        if(ret == 1) {
            return new Result<>(1, "移除喜欢的歌曲成功", true);
        }else {
            return new Result<>(-1, "移除喜欢的歌曲失败", false);
        }
    }
}
